home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / maketabl.fr_ / maketabl.fr
Text File  |  1995-07-04  |  6KB  |  216 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Table Maker"
  5.    ClientHeight    =   5340
  6.    ClientLeft      =   2025
  7.    ClientTop       =   1560
  8.    ClientWidth     =   7125
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   0
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   5745
  19.    Left            =   1965
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   5340
  22.    ScaleWidth      =   7125
  23.    Top             =   1215
  24.    Width           =   7245
  25.    Begin VB.ListBox lstData 
  26.       Height          =   3735
  27.       Left            =   480
  28.       Sorted          =   -1  'True
  29.       TabIndex        =   3
  30.       Top             =   420
  31.       Width           =   6135
  32.    End
  33.    Begin VB.CommandButton cmdClose 
  34.       Cancel          =   -1  'True
  35.       Caption         =   "Close"
  36.       Height          =   615
  37.       Left            =   4980
  38.       TabIndex        =   2
  39.       Top             =   4440
  40.       Width           =   1635
  41.    End
  42.    Begin VB.CommandButton cmdDropTable 
  43.       Caption         =   "D&rop Table"
  44.       Height          =   615
  45.       Left            =   2760
  46.       TabIndex        =   1
  47.       Top             =   4440
  48.       Width           =   1635
  49.    End
  50.    Begin VB.CommandButton cmdCreateTable 
  51.       Caption         =   "&Create Table"
  52.       Height          =   615
  53.       Left            =   480
  54.       TabIndex        =   0
  55.       Top             =   4440
  56.       Width           =   1635
  57.    End
  58. End
  59. Attribute VB_Name = "Form1"
  60. Attribute VB_Creatable = False
  61. Attribute VB_Exposed = False
  62. Option Explicit
  63.  
  64. Private Sub Form_Load()
  65.     Dim db As DATABASE
  66.     Dim dbName As String
  67.     Dim td As TableDef
  68.     Dim tableFound As Boolean
  69.  
  70.     On Error GoTo LoadError
  71.  
  72.     tableFound = False
  73.  
  74.     ' Get the database name and open the database.
  75.     dbName = BiblioPath()       ' BiblioPath is a function in READINI.BAS
  76.     Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
  77.     
  78.     ' Cycle through the table definitions in DATABASE_NAME. If a table named
  79.     ' Publisher Titles is found, initialize the Table command buttons and
  80.     ' fill the list box with the data in the table.
  81.     For Each td In db.TableDefs
  82.         If td.Name = "Publisher Titles" Then
  83.             tableFound = True
  84.             cmdDropTable.Enabled = True
  85.             cmdCreateTable.Enabled = False
  86.             FillList
  87.             Exit For
  88.         End If
  89.     Next
  90.  
  91.     ' The table does not exist, so set the command buttons.
  92.     If tableFound = False Then
  93.         cmdDropTable.Enabled = False
  94.         cmdCreateTable.Enabled = True
  95.     End If
  96.  
  97. Exit Sub
  98.  
  99. LoadError:
  100.     MsgBox Error$, vbExclamation
  101.     Unload Me
  102. Exit Sub
  103.  
  104. End Sub
  105.  
  106. Sub FillList()
  107.     Dim db As DATABASE
  108.     Dim dbName As String
  109.     Dim rs As Recordset
  110.     Dim sql As String
  111.  
  112.     On Error GoTo FillListError
  113.     lstData.Clear
  114.     
  115.     ' Get the database name and open the database.
  116.     dbName = BiblioPath()       ' BiblioPath is a function in READINI.BAS
  117.     Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
  118.     
  119.     Set rs = db.OpenRecordset("SELECT * FROM [Publisher Titles]", dbOpenSnapshot)
  120.     If rs.RecordCount > 0 Then
  121.         rs.MoveFirst
  122.         Do
  123.             lstData.AddItem rs![Name] & ": " & rs![Title]
  124.             rs.MoveNext
  125.         Loop While Not rs.EOF
  126.     End If
  127.  
  128. Exit Sub
  129.  
  130. FillListError:
  131.     MsgBox Error$, vbExclamation
  132. Exit Sub
  133.  
  134. End Sub
  135.  
  136. Private Sub cmdCreateTable_Click()
  137.     Dim db As DATABASE
  138.     Dim dbName As String
  139.     Dim sql As String
  140.  
  141.     On Error GoTo CreateTableError
  142.  
  143.     Screen.MousePointer = 11
  144.  
  145.     ' Get the database name and open the database.
  146.     dbName = BiblioPath()       ' BiblioPath is a function in READINI.BAS
  147.     Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
  148.  
  149.     ' Build the SQL statement.
  150.     ' The SELECT clause names the fields from the source recordset to be
  151.     ' created in the new table.
  152.     sql = "SELECT Publishers.Name, Titles.Title"
  153.  
  154.     ' The INTO clause names the new table.
  155.     sql = sql & " INTO [Publisher Titles]"
  156.  
  157.     ' The FROM clause names the table or tables to be used as the source
  158.     ' of the data for the new table.
  159.     sql = sql & " FROM Publishers INNER JOIN Titles"
  160.     sql = sql & " ON Publishers.PubID = Titles.PubID"
  161.  
  162.     ' Create the new table by executing the SQL statement.
  163.     db.Execute (sql)
  164.  
  165.     ' Fill the list box with records.
  166.     FillList
  167.  
  168.     ' Set the command buttons.
  169.     cmdCreateTable.Enabled = False
  170.     cmdDropTable.Enabled = True
  171.  
  172.     Screen.MousePointer = 0
  173.  
  174. Exit Sub
  175.  
  176. CreateTableError:
  177.     Screen.MousePointer = 0
  178.     MsgBox Error$, vbExclamation
  179. Exit Sub
  180.  
  181. End Sub
  182.  
  183.  
  184. Private Sub cmdDropTable_Click()
  185.     Dim db As DATABASE
  186.     Dim dbName As String
  187.  
  188.     On Error GoTo DropTableError
  189.     
  190.     ' Get the database name and open the database.
  191.     dbName = BiblioPath()       ' BiblioPath is a function in READINI.BAS
  192.     Set db = DBEngine.Workspaces(0).OpenDatabase(dbName)
  193.  
  194.     ' Delete the table and set the command buttons.
  195.     db.Execute ("DROP TABLE [Publisher Titles]")
  196.     cmdDropTable.Enabled = False
  197.     cmdCreateTable.Enabled = True
  198.  
  199.     ' Clear the list box.
  200.     lstData.Clear
  201.  
  202. Exit Sub
  203.  
  204. DropTableError:
  205.     MsgBox Error$, vbExclamation
  206. Exit Sub
  207.  
  208. End Sub
  209.  
  210.  
  211. Private Sub cmdClose_Click()
  212.     End
  213. End Sub
  214.  
  215.  
  216.